package com.mangguo.tingjian.common;

public class Result<T> {
    private final static int SUCCESS = 200;
    private final static int ERROR = 500;
    private String msg;
    private int status;
    private T data;

    private Result(){

    }

    private Result(T data) {
        this.status = SUCCESS;
        this.msg = "success";
        this.data = data;
    }
    private Result(ResultStatus resultStatus) {
        if(resultStatus == null){
            return;
        }
        this.status = resultStatus.getStatus();
        this.msg = resultStatus.getMessage();
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static <T> Result<T> success(T data){
        return new Result<T>(data);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(){
        return (Result<T>) success("");
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> Result<T> error(ResultStatus rs){
        return new Result<T>(rs);
    }

    public static <T> Result<T> error(String msg){
        Result<T> result = new Result<T>();
        result.status = ERROR;
        result.msg = msg;
        return result;
    }

    /**
     * 失败时候的调用,扩展消息参数
     * @param rs
     * @param msg
     * @return
     */
    public static <T> Result<T> error(ResultStatus rs,String msg){
        rs.setMessage(rs.getMessage()+"--"+msg);
        return new Result<T>(rs);
    }
    public T getData() {
        return data;
    }
    public String getMsg() {
        return msg;
    }
    public int getStatus() {
        return status;
    }
}
